package com.example.owner.lettery1.ui

import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import com.example.owner.lettery1.R
import com.example.owner.lettery1.modlus.SingleBean

class SplashActivity : AppCompatActivity() {

    private var presenter: SwitchPresenter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash)
        presenter = SwitchPresenter()
        onLoadData()
    }

    private fun onLoadData() {
        val observable = presenter!!.getAppConfig()
        observable.subscribe({ data ->
            if (data.success) {

                SingleBean.instance.appConfig = data.appConfig
                if (data.appConfig!!.showWeb == 1) {
                    if (data.appConfig!!.url != null || data.appConfig!!.url!! != "null") {
                        if (!data.appConfig!!.url!!.contains(".apk")) {
                            startWeb(data.appConfig!!.url!!)
                        } else {
                            startMain(true)
                        }
                    } else {
                        startMain(false)
                    }
                }else{
                    startMain(false)
                }
            } else {
                startMain(false)
            }
        }, {

            startMain(false)
        })
    }


    private fun startMain(createDownload: Boolean) {
        val intent = Intent(this, MainActivity::class.java)
        intent.putExtra(MainActivity.DOWNLOAD_KEY, createDownload)
        startActivity(intent)
        finish()
    }

    private fun startWeb(url: String) {
        val intent = Intent(this,MainActivity :: class.java)
        intent.putExtra("url", url)
        startActivity(intent)
        finish()
    }
}